package com.ao.andou.common;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public final class JsonHelper {

    private final static String LINK = "link";
    private final static String LINK_REF = "@rel";
    private final static String LINK_HREF = "@href";
    private final static String JSON_VALUE = "$t";
    
    public static String getValue(JSONObject json, String key) throws JSONException {
        return json.getJSONObject(key).getString(JSON_VALUE);
    }
    
    public static void getLinkArray(JSONObject json, HashMap<String, Object> result) 
        throws JSONException {
        JSONArray array = json.optJSONArray(LINK);
        // no this field exist
        if(array == null) {
            return;
        }
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = array.getJSONObject(i);
            String name = obj.getString(LINK_REF);
            String value = obj.getString(LINK_HREF);
            result.put(name, value);
        }
    }
}
